﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;

namespace BIZ.Common
{
   public static class ListUrlToolcs
    {
        private static string addParam(string sParamName, string sParamValue, string sDefalueValue, bool bEncode)
        {
            if (string.IsNullOrEmpty(sParamName)) return "";
            if (string.IsNullOrEmpty(sParamValue) && string.IsNullOrEmpty(sDefalueValue)) return "";
            else if (!string.IsNullOrEmpty(sParamValue))
            {
                if (sParamValue != "0")
                    return "&" + sParamName + "=" + (bEncode ? System.Web.HttpUtility.UrlEncode(sParamValue) : sParamValue);
                else
                    return "";
            }
            else
                return "&" + sParamName + "=" + sDefalueValue;
        }

        private static string addParam(string sParamName, string sParamValue, string sDefalueValue)
        {
            return addParam(sParamName, sParamValue, sDefalueValue, false);
        }
        
        public static string locateSearch(HttpRequest request,
            string e,string l,string z,string le,string y,string x,string cd,string mp3,string inventory,string tt,
            int? pageno)
        {
            string _query = "";
            // 对页码编号变化特殊处理，其他条件变化时，不需要加pageid参数
            if (pageno != null)
            {
                _query = request.Url.Query;

                if (string.IsNullOrEmpty(request.QueryString["pageid"]))
                    _query += addParam("pageno", pageno.ToString(), "");
                else
                    _query = _query.Replace("pageno=" + request.QueryString["pageno"], "pageno=" + pageno.ToString());
                return _query.Length > 0 ? "?" + _query.Substring(1) : "";
            }
            if (string.IsNullOrEmpty(tt))
                _query += addParam("tt", tt, request.QueryString["tt"], true);
            _query += addParam("e", e, request.QueryString["e"]);
            _query += addParam("l", l, request.QueryString["l"]);
            _query += addParam("z", z, request.QueryString["z"]);
            _query += addParam("le", le, request.QueryString["le"]);
            _query += addParam("y", y, request.QueryString["y"]);
            _query += addParam("x", x, request.QueryString["x"]);
            _query += addParam("cd", cd, request.QueryString["cd"]);
            _query += addParam("mp3", mp3, request.QueryString["mp3"]);
            _query += addParam("inventory",inventory , request.QueryString["inventory"]);
            return _query.Length > 0 ? "?" + _query.Substring(1) : "";
        }
    }
}
